﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Web.UI.HtmlControls;
using System.IO;

namespace WebSys.UI.Control.ZhengShou
{
    public class FileUploadControl : MPBasicTemplate
    {
        LinkButton LinkButton1, LinkButton2, LinkButton3;
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LinkButton1 = (LinkButton)FindControl("LinkButton1");
            LinkButton2 = (LinkButton)FindControl("LinkButton2");
            LinkButton3 = (LinkButton)FindControl("LinkButton3");
            LinkButton1.Click += new EventHandler(LinkButton1_Click);
            LinkButton2.Click += new EventHandler(LinkButton2_Click);
            LinkButton3.Click += new EventHandler(LinkButton3_Click);
            if (!base.Page.IsPostBack)
            {
                //Repeater Repeater1 = (Repeater)FindControl("Repeater1");
                //List<EluZSProvider.File> file = _EluZSDataProvider.GetFile(HttpContext.Current.Request["PID"], "1", "");
                //Repeater1.DataSource = file;
                //Repeater1.DataBind();
                Repeater Repeater1 = (Repeater)FindControl("Repeater1");
                Repeater fujianlist = (Repeater)FindControl("fujianlist");
                fujianlist.ItemDataBound += new RepeaterItemEventHandler(fujianlist_ItemDataBound);
                Repeater1.Visible = false;
                fujianlist.Visible = true;
                List<EluZSProvider.File> file = _EluZSDataProvider.GetFile(HttpContext.Current.Request["PID"], "", "土地权属证明文件");
                fujianlist.DataSource = file;
                fujianlist.DataBind();
            }
            Bind();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Repeater Repeater1 = (Repeater)FindControl("Repeater1");
            Repeater fujianlist = (Repeater)FindControl("fujianlist");
            Repeater1.Visible = false;
            fujianlist.Visible = true;
            List<EluZSProvider.File> file = _EluZSDataProvider.GetFile(HttpContext.Current.Request["PID"], "", "项目批准文件");
            fujianlist.DataSource = file;
            fujianlist.DataBind();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Repeater Repeater1 = (Repeater)FindControl("Repeater1");
            Repeater fujianlist = (Repeater)FindControl("fujianlist");
            Repeater1.Visible = false;
            fujianlist.Visible = true;
            List<EluZSProvider.File> file = _EluZSDataProvider.GetFile(HttpContext.Current.Request["PID"], "", "规划许可文件");
            fujianlist.DataSource = file;
            fujianlist.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Repeater Repeater1 = (Repeater)FindControl("Repeater1");
            Repeater fujianlist = (Repeater)FindControl("fujianlist");
            Repeater1.Visible = false;
            fujianlist.Visible = true;
            List<EluZSProvider.File> file = _EluZSDataProvider.GetFile(HttpContext.Current.Request["PID"], "", "土地权属证明文件");
            fujianlist.DataSource = file;
            fujianlist.DataBind();
        }
        protected void Bind()
        {
            
            Repeater fujianlist = (Repeater)FindControl("fujianlist");
            Button back = (Button)FindControl("back");
            Button upfile = (Button)FindControl("upfile");
            Button Button1 = (Button)FindControl("Button1");
            back.Click += new EventHandler(back_Click);
            upfile.Click += new EventHandler(upfile_Click);
            Button1.Click += new EventHandler(button1_Click);
            fujianlist.ItemDataBound+=new RepeaterItemEventHandler(fujianlist_ItemDataBound);
            fujianlist.Visible = true;
            back.Visible = false;
            Repeater Repeater1 = (Repeater)FindControl("Repeater1");
            Repeater1.Visible = false;
            foreach (RepeaterItem rp in Repeater1.Items)
            {
                LinkButton dkfj = (LinkButton)rp.FindControl("dkfj");
                dkfj.Click += new EventHandler(dkfj_Click);
            }

            if (!string.IsNullOrEmpty(HttpContext.Current.Request["flag"]))
            {
                if (HttpContext.Current.Request["flag"] == "show")
                {
                    upfile.Visible = false;
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "displaytable", "<script>displaytable();</script>");
                    //Label Label3 = (Label)e.Item.FindControl("Label3");
                    foreach (RepeaterItem e in fujianlist.Items)
                    {
                        Label Label3 = (Label)e.FindControl("Label3");
                        Label3.Visible = false;
                    }
                }
            }
            if (!string.IsNullOrEmpty(HttpContext.Current.Request["action"])) {
                if (HttpContext.Current.Request["action"] == "del") {
                    _EluZSDataProvider.DeleteXmByID("ZS_FILE", "ID", HttpContext.Current.Request["id"]);
                    HttpContext.Current.Response.Write("<script>alert('文件删除成功！');window.location.href('FileUpload.aspx?PID=" + HttpContext.Current.Request["PID"] + "');</script>");
                }
            }
        }
        protected void upfile_Click(object sender, EventArgs e)
        {
            FileUpload picFile = (FileUpload)FindControl("picFile");
            TextBox filename = (TextBox)FindControl("filename");
            DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
            if (picFile.HasFile)
            {
                HttpPostedFile postfile = picFile.PostedFile;
                string fileName = Path.GetFileName(postfile.FileName);
                string fileext = fileName.Substring(fileName.LastIndexOf("."));
                if ((fileext.ToLower() == ".jpg") || (fileext.ToLower() == ".gif") || (fileext.ToLower() == ".bmp") || (fileext.ToLower() == ".jpeg")
                    || (fileext.ToLower() == ".doc") || (fileext.ToLower() == ".docx") || (fileext.ToLower() == ".xls"))
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(postfile.FileName))
                        {
                            Stream filestream = postfile.InputStream;
                            byte[] bFileContent = new byte[filestream.Length];
                            if (bFileContent.Length > 5242880)
                            {
                                HttpContext.Current.Response.Write("<script>alert('上传文件大于5M！');window.location.href=window.location.href;</script>");
                            }
                            else
                            {
                                if (bFileContent.Length > 0)
                                {
                                    filestream.Read(bFileContent, 0, bFileContent.Length);
                                    string guidstr = Guid.NewGuid().ToString().Replace("-", "").ToUpper();
                                    string filenametemp = filename.Text;
                                    if (string.IsNullOrEmpty(filenametemp))
                                    {
                                        filenametemp = fileName.Replace(fileext, "");
                                    }
                                    _EluZSDataProvider.UpdateFileContent("ZS_FILE", guidstr, bFileContent, bFileContent.Length, HttpContext.Current.Request["PID"], filenametemp, fileext, DropDownList1.SelectedValue, "1");
                                    HttpContext.Current.Response.Write("<script>alert('文件上传成功！');window.location.href=window.location.href;</script>");
                                }
                            }
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("<script>alert('文件名不得为空！');history.back();</script>");
                        }
                    }
                    catch
                    {
                        HttpContext.Current.Response.Write("<script>alert('上传出错！');history.back();</script>");
                    }
                }
                else
                {
                    HttpContext.Current.Response.Write("<script>alert('上传文件只支持jpg,gif,jpeg,bmp,doc,docx,xls格式');history.back();</script>");
                }
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('请选择上传文件！');history.back();</script>");
            }
        }

        protected void dkfj_Click(object sender, EventArgs e)
        {
            Repeater Repeater1 = (Repeater)FindControl("Repeater1");
            Repeater fujianlist = (Repeater)FindControl("fujianlist");
            Button back = (Button)FindControl("back");
            LinkButton bt = sender as LinkButton;
            RepeaterItem rptItem = bt.Parent as RepeaterItem;
            HiddenField pkName = rptItem.FindControl("HiddenField2") as HiddenField;
            string itemName = pkName.Value;
            Repeater1.Visible = false;
            fujianlist.Visible = true;
            back.Visible = true;

            List<EluZSProvider.File> file = _EluZSDataProvider.GetFile(HttpContext.Current.Request["PID"], "", itemName);
            fujianlist.DataSource = file;
            fujianlist.DataBind();
        }

        protected void back_Click(object sender, EventArgs e)
        {
            Repeater Repeater1 = (Repeater)FindControl("Repeater1");
            Repeater fujianlist = (Repeater)FindControl("fujianlist");
            Button back = (Button)FindControl("back");
            Repeater1.Visible = true;
            fujianlist.Visible = false;
            back.Visible = false;
        }
        protected void button1_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ProjectList.aspx");
        }
        protected void fujianlist_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label Label3 = (Label)e.Item.FindControl("Label3");
            string tt = HttpContext.Current.Request["flag"];
            if (!string.IsNullOrEmpty(tt))
            {
                if (tt == "show")
                {
                    Label3.Visible = false;
                }
            }
        }
    }
}


